package com.zhika.zishi.fragments.login;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhika.zishi.R;
import com.zhika.zishi.activity.RecommendHomepageActivity;
import com.zhika.zishi.adapter.AnswererAdapter;
import com.zhika.zishi.beans.AnswererBean;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by shi-02 on 2015/5/2.
 * 注册的时候，选择关注的用户
 */
public class RegisterUserFragment extends Fragment implements View.OnClickListener{

    private View view;

    private GridView gridView;

    private ArrayList<AnswererBean> userList= new ArrayList<>();

    private AnswererAdapter adapter;

    private Context context;

    private HashMap<Integer,Boolean> selectUserHash = new HashMap<>();

    //跳过
    private TextView skipText;

    private Button sureButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.register_user_layout,container,false);

        addTestData();
        initView();

        return view;
    }

    /**
     * 初始化界面的控件
     */
    private void initView(){
        context = getActivity();

        gridView = (GridView) view.findViewById(R.id.register_user_gird_view);
        adapter = new AnswererAdapter(context,userList);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ImageView imageView = (ImageView) view.findViewById(R.id.item_of_my_gird_view_state_image_view);

                if (selectUserHash.get(position)==null || !selectUserHash.get(position)){
                    imageView.setBackgroundColor(getResources().getColor(R.color.color_red));
                    selectUserHash.put(position,true);
                }else {
                    imageView.setBackgroundColor(getResources().getColor(R.color.identify_false_color));
                    selectUserHash.put(position,false);
                }
            }
        });

        skipText = (TextView) view.findViewById(R.id.register_user_skip_text);
        skipText.setOnClickListener(this);

        sureButton= (Button) view.findViewById(R.id.register_user_sure_button);
        sureButton.setOnClickListener(this);
    }

    private void addTestData(){
        for (int i = 0;i<9;i++){
            AnswererBean bean = new AnswererBean();
            bean.setType("register");
            bean.setNicknameStr("怪蜀黍");
            bean.setIntroductionStr("个人简介");

            userList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register_user_skip_text:
                entryHomepage(false);
                break;
            case R.id.register_user_sure_button:
                entryHomepage(true);
                break;
        }
    }

    /**
     * 进入主页面
     * @param b
     */
    private void entryHomepage(boolean b){
        Intent intent = new Intent(context, RecommendHomepageActivity.class);
        //确定按钮的事件
        if (b){
            //添加一些数据
//            intent.putExtra()
        }
        //跳过按钮的事件
        else {

        }
        context.startActivity(intent);
    }
}
